import { removeToken, setToken } from '@/common/auth'

const state = {
  token: '', // token
}

const mutations = {
  CHANGE_SETTING: (state, obj) => {
    Object.keys(obj).forEach((key) => {
      if (Object.prototype.hasOwnProperty.call(state, key)) {
        state[key] = obj[key]
        if (key === 'token') {
          setToken(obj[key])
        }
      }
    })
  },
  CLEAR_INFO: (state, whiteList) => {
    if (!whiteList) whiteList = []
    Object.keys(state).forEach((key) => {
      if (whiteList.indexOf(key) === -1) {
        if (Array.isArray(state[key])) {
          state[key] = []
        } else if (typeof state[key] === 'object') {
          state[key] = {}
        } else {
          state[key] = ''
        }
        if (key === 'token') {
          removeToken()
        }
      }
    })
  },
}

const actions = {
  changeSetting({ commit }, data) {
    commit('CHANGE_SETTING', data)
  },
  //   getUserInfo({ commit, state }) {},
  //   login({ commit, state }, userInfo) {},
  //   logout({ commit, state, dispatch, rootState }) {},
  clearInfo({ commit }) {
    removeToken()
    commit('CLEAR_INFO')
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
